package com.hula.onion.book.server.spider.platforms.guangxindai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/1/29.
 */
@Component
public class GuangXinDaiSpiderHelper {
    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("username", form.getUserName());
        map.put("password", form.getPassword());
        map.put("err", "");
        map.put("verifycode", "");
        map.put("auto_login", "0");
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public CommonResponse parseAccountBalance(String json) {
        if (StringUtils.isEmpty(json)) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        JSONObject accountJson = JSON.parseObject(json);
        if ("200".equals(accountJson.getString("status_code"))) {
            JSONObject contentJson = accountJson.getJSONObject("content");
            if (contentJson == null) {
                return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
            }
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setAmount(contentJson.getBigDecimal("total_account"));
            JSONObject interestJson = contentJson.getJSONObject("user_interest");
            accountInfo.setTotalIntrest(interestJson.getBigDecimal("total_interest"));
            accountInfo.setWaitIntrest(interestJson.getBigDecimal("wait_interest_total"));
            return new CommonResponse(accountInfo);
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
